package com.itheima.reggie.controller;

import com.itheima.reggie.common.PageBean;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private HttpSession session;

    /**
     * 查询分页数据
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<PageBean> findPage(int page,int pageSize){
        return categoryService.findByPage(page,pageSize);
    }

    /**
     *  新增分类或套餐
     * @param category
     * @return
     */
    @PostMapping
    public R<String> addCategory(@RequestBody Category category){
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser((Long) session.getAttribute("employee"));
        category.setUpdateUser((Long) session.getAttribute("employee"));
        R<String> result =  categoryService.addCategory(category);
        return result;
    }

    /**
     * 更新分类或套餐
     * @param category
     * @return
     */
    @PutMapping
    public R<String> updateCategory(@RequestBody Category category){
        category.setUpdateUser((Long) session.getAttribute("employee"));
        category.setUpdateTime(LocalDateTime.now());
        R<String> result = categoryService.updateCategory(category);
        return  result;
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteCategory(Long id){
        return categoryService.deleteCategoryById(id);
    }

    /**
     * 查询分类名称
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> findCategoryNameList(Integer type){
        return categoryService.findCategoryByType(type);
    }



}
